//
//  YXLocalized.m
//  YXInternational
//
//  Created by GMS on 2020/8/28.
//  Copyright © 2020 Arhtur. All rights reserved.
//

#import "YXLocalized.h"
#import "NSBundle+Language.h"

@implementation YXLocalized

+ (YXLocalized *)sharedInstance {
    static YXLocalized *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[YXLocalized alloc] init];
    });
    return instance;
}

- (void)initLanguage{
    NSString *language=[self currentLanguage];
    if (language.length>0) {
        NSLog(@"自设置语言:%@",language);
        [self setLanguage:language];
    }else{
        [self systemLanguage];
    }
}
- (NSString *)currentLanguage{
    NSString *language=[[NSUserDefaults standardUserDefaults]objectForKey:AppLanguage];
    return language;
}
- (void)setLanguage:(NSString *)language{
    [NSBundle setLanguage:language];
    
    [[NSUserDefaults standardUserDefaults] setObject:language forKey:AppLanguage];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

// MARK: 设置初始化语言，只有简体中文和英文两种
- (void)systemLanguage{
    NSString *languageCode = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"][0];
    NSLog(@"系统语言:%@",languageCode);
    if([languageCode hasPrefix:@"zh-Han"]){
        languageCode = @"zh-Hans"; //中文全是简体中文
    } else {
        languageCode = @"en";//英语
    }
    [self setLanguage:languageCode];
}



@end
